﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFHostDemoShell
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        IntPtr childWindowHandle = IntPtr.Zero;

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            HwndSource.FromHwnd(new WindowInteropHelper(this).Handle).AddHook(HwndSourceHook);
        }

        protected IntPtr HwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
        {
            switch(msg)
            {
                case User32.WM_MOVE:
                    User32.SendMessage(childWindowHandle, User32.WM_Pos, wParam, lParam);
                    break;
            }

            return IntPtr.Zero;
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var childExePath = Environment.CurrentDirectory + "\\ChildWindow.exe";
            if (System.IO.File.Exists(childExePath) == false)
                return;

            var process = System.Diagnostics.Process.Start(childExePath);

            await Task.Delay(500);

            var point = this.Host.PointToScreen(new Point(0, 0));

            User32.SetWindowPos(process.MainWindowHandle, IntPtr.Zero, (int)point.X, (int)point.Y, (int)this.Host.ActualWidth, (int)this.Host.ActualHeight, User32.SWP_SHOWWINDOW);
            childWindowHandle = process.MainWindowHandle;

            User32.SendMessage(childWindowHandle, User32.WM_ShowInTaskBar, IntPtr.Zero, IntPtr.Zero);
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Normal)
            {
                User32.SendMessage(childWindowHandle, User32.WM_Normal, IntPtr.Zero, IntPtr.Zero);
            }

            if (this.WindowState == WindowState.Minimized)
            {
                User32.SendMessage(childWindowHandle, User32.WM_Minimal, IntPtr.Zero, IntPtr.Zero);
            }
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            User32.SendMessage(childWindowHandle, User32.WM_Size, (IntPtr)Host.ActualWidth, (IntPtr)Host.ActualHeight);
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            User32.SendMessage(childWindowHandle, User32.WM_Exit, IntPtr.Zero, IntPtr.Zero);
        }
    }
}
